#!/bin/bash

# 检查是否以 root 用户运行
if [ "$(id -u)" != "0" ]; then
   echo "此脚本必须以 root 用户运行" 1>&2
   exit 1
fi

# 安装依赖项
echo "安装依赖项..."
apt update
apt install -y build-essential git libevent-dev libssl-dev libyaml-dev libjson-c-dev

# 检查是否需要安装 libcyaml-dev
if ! dpkg -l | grep -q libcyaml-dev; then
    echo "安装 libcyaml-dev..."
    apt install -y libcyaml-dev
fi

# 如果 libcyaml-dev 不可用，尝试从源码编译安装
if ! dpkg -l | grep -q libcyaml-dev; then
    echo "从源码安装 libcyaml..."
    apt install -y libcdoc-dev
    cd /tmp
    git clone https://github.com/tlsa/libcyaml.git
    cd libcyaml
    make
    make install
    cd ..
    rm -rf libcyaml
    cd -
fi

# 创建目录结构
echo "创建目录结构..."
mkdir -p /usr/local/etc/2rtk
if [ $? -ne 0 ]; then
    echo "创建配置文件目录失败" 1>&2
    exit 1
fi
mkdir -p /var/log/2rtk
if [ $? -ne 0 ]; then
    echo "创建日志目录失败" 1>&2
    exit 1
fi

# 编译 caster
echo "编译 caster..."
cd caster
make clean
make
if [ $? -ne 0 ]; then
    echo "编译失败，请检查错误信息" 1>&2
    exit 1
fi
cd ..

# 安装二进制文件
echo "安装二进制文件..."
if [ ! -f caster/caster ]; then
    echo "编译后的二进制文件不存在，请检查编译过程" 1>&2
    exit 1
fi
install -m 755 caster/caster /usr/local/sbin/
if [ $? -ne 0 ]; then
    echo "安装二进制文件失败" 1>&2
    exit 1
fi

# 安装配置文件
echo "安装配置文件..."
if [ ! -f /usr/local/etc/2rtk/caster.yaml ]; then
    install -m 644 2rtk-config/caster.yaml /usr/local/etc/2rtk/
    # 修改配置文件中的 blocklist_file 路径为绝对路径
    sed -i 's|blocklist_file:	blocklist|blocklist_file:	/usr/local/etc/2rtk/blocklist|g' /usr/local/etc/2rtk/caster.yaml
fi
if [ ! -f /usr/local/etc/2rtk/sourcetable.dat ]; then
    install -m 644 2rtk-config/sourcetable.dat /usr/local/etc/2rtk/
fi
if [ ! -f /usr/local/etc/2rtk/source.auth ]; then
    install -m 644 2rtk-config/source.auth /usr/local/etc/2rtk/
fi
if [ ! -f /usr/local/etc/2rtk/host.auth ]; then
    install -m 644 2rtk-config/host.auth /usr/local/etc/2rtk/
fi
if [ ! -f /usr/local/etc/2rtk/blocklist ]; then
    install -m 644 2rtk-config/blocklist /usr/local/etc/2rtk/
fi

# 创建日志文件并设置权限
touch /var/log/2rtk/access.log
touch /var/log/2rtk/caster.log
chown caster:caster /var/log/2rtk/access.log
chown caster:caster /var/log/2rtk/caster.log

# 创建 caster 用户和组
echo "创建 caster 用户和组..."
if ! getent group caster > /dev/null; then
    groupadd -r caster
fi
if ! getent passwd caster > /dev/null; then
    useradd -r -g caster -d /var/lib/caster -s /sbin/nologin -c "2RTK Caster" caster
    mkdir -p /var/lib/caster
    chown caster:caster /var/lib/caster
fi

# 设置权限
echo "设置权限..."
# 配置文件属于 root，但确保特定文件有适当的权限
chown -R root:root /usr/local/etc/2rtk
chmod 644 /usr/local/etc/2rtk/*
# 特定配置文件需要更严格的权限
chmod 640 /usr/local/etc/2rtk/source.auth
chmod 640 /usr/local/etc/2rtk/host.auth
chmod 640 /usr/local/etc/2rtk/blocklist
# 日志目录属于 caster 用户
chown -R caster:caster /var/log/2rtk
chmod 755 /var/log/2rtk

# 安装 systemd 服务
echo "安装 systemd 服务..."
if [ ! -f 2rtk-caster.service ]; then
    echo "systemd 服务文件不存在" 1>&2
    exit 1
fi
install -m 644 2rtk-caster.service /etc/systemd/system/
if [ $? -ne 0 ]; then
    echo "安装 systemd 服务文件失败" 1>&2
    exit 1
fi
systemctl daemon-reload
systemctl enable 2rtk-caster.service

echo "安装完成！"
echo "使用以下命令启动服务："
echo "  systemctl start 2rtk-caster.service"
echo "使用以下命令检查服务状态："
echo "  systemctl status 2rtk-caster.service"
echo "使用以下命令查看日志："
echo "  journalctl -u 2rtk-caster.service"